package com.controller;

import com.bean.Cinema;
import com.bean.Seat;
import com.service.CinemaService;
import com.service.SeatService;
import com.util.PageAssistant;
import com.vo.CinemaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cinema")
public class CinemaController {
    @Autowired
    private CinemaService cinemaService;

    @Autowired
    private SeatService seatService;

    @RequestMapping("query")
    public String query(HttpSession session, Model model, Cinema cinema, PageAssistant<Cinema> paCna){
        System.out.println(">>>>>>>>>>> cinema query");
        System.out.println(paCna);
        System.out.println("-------------------");
        msg(session,model);

        Map<String,Object> map = new HashMap<>();
        map.put("start",paCna.getStart());
        map.put("size",paCna.getSize());

        paCna.setCount(cinemaService.count(map));
        paCna.setItems(cinemaService.query(map));

        model.addAttribute("paCna",paCna);

        return "page/cinema/cinemaList";
    }

    @RequestMapping("add")
    public String add(HttpSession session,Model model){
        msg(session,model);
        return "page/cinema/add";
    }

    @RequestMapping("save")
    public String save(HttpSession session, Model model, CinemaVo cinema){
        System.out.println(">>>>>>>> cinema save");
        System.out.println(cinema);
        System.out.println("--------------------------");
        List<Cinema> query = cinemaService.query(new HashMap<>());

        for (Cinema c: query) {
            if(cinema.getHall().equals(c.getHall())){
                session.setAttribute("msg","影厅名不能重复");
                return "redirect:/cinema/add";
            }
        }

        if(cinema.getType().equals("0")){
            cinema.setSeat("348");
        }else{
            cinema.setSeat("187");
        }
        int save = cinemaService.save(cinema.parse());
        if(save == 1){
            session.setAttribute("msg","添加成功");
            //根据影厅名查询到刚添加的影厅信息
            Cinema cinema1 = cinemaService.find(new Cinema(cinema.getHall())).get(0);
            session.setAttribute("cinema1",cinema1);
            System.out.println("cinema1"+cinema1);
            Seat seat = new Seat();
            if(cinema.getType().equals("0")){
                for (int i = 0; i <12 ; i++) {
                    for (int j = 0; j <29 ; j++) {
                        seat.setCinemaId(cinema1.getId());
                        seat.setRows(i+1);
                        seat.setNumber(j+1);
                        seat.setState(0);
                        seatService.save(seat);
                    }
                }
            }else{
                for (int i = 0; i <11 ; i++) {
                    for (int j = 0; j <17 ; j++) {
                        seat.setCinemaId(cinema1.getId());
                        seat.setRows(i+1);
                        seat.setNumber(j+1);
                        seat.setState(0);
                        seatService.save(seat);
                    }
                }
            }
            return "redirect:/cinema/unique";
        }else{
            model.addAttribute("msg","添加失败");
        }

        return  "page/cinema/add";
    }
    @RequestMapping("unique")
    public String unique(HttpSession session, Model model, Cinema cinema){
        System.out.println(">>>>>>>> cinema unique");
        System.out.println(cinema.getHall());
        System.out.println("--------------------------");
        msg(session,model);
        if(session.getAttribute("cinema1") != null){
            model.addAttribute("cinemaList",session.getAttribute("cinema1"));
            return "page/cinema/see";
        }else{
            List<Cinema> cinemaList = cinemaService.find(cinema);
            if(cinemaList.size() == 1){
                model.addAttribute("cinemaList",cinemaList.get(0));
                return "page/cinema/see";
            }
        }
        return null;
    }
    @RequestMapping("update")
    public String update(Model model, HttpSession session, CinemaVo cinema){
        System.out.println(">>>>>>>> cinema update");
        System.out.println(cinema);
        System.out.println("--------------------------");

        if(cinema.getType().equals("0")){
            cinema.setSeat("348");
        }else{
            cinema.setSeat("187");
        }
        int modify = cinemaService.modify(cinema.parse());

        if(modify == 1){
            session.setAttribute("msg","修改成功");
        }else{
            session.setAttribute("msg","修改失败");
        }

        return "redirect:/cinema/unique?id="+cinema.getId();
    }
    @RequestMapping("delete")
    public String delete(HttpSession session, Cinema cinema){
        System.out.println(">>>>>>>> cinema delete");
        System.out.println(cinema);
        System.out.println("--------------------------");
        int remove = cinemaService.remove(cinema);
        System.out.println(remove);
        if(remove == 1){
            session.setAttribute("msg","删除成功");
        }else {
            session.setAttribute("msg","删除失败");
        }
        return "redirect:/cinema/query";
    }

    private void msg(HttpSession session,Model model){
        if(session.getAttribute("msg")!= null){
            model.addAttribute("msg",session.getAttribute("msg"));
            session.removeAttribute("msg");
        }
    }
}
